<template>
	<el-container>
		<el-header>
			<div>
				<img src="../assets/logo.png" alt="" />
				<span>电商后台管理系统</span>
			</div>
			<el-button type="info" @click="loginOut">退出</el-button>
		</el-header>
		<el-container>
			<!-- 侧别栏菜单区域 -->
			<el-aside :width="isCollapse ? '64px': '200px'">
				<div class="toggle-button" @click="toggleCollapse">|||</div>
				<el-menu 
					background-color="#373d41"
					text-color="#fff"
					active-text-color="#409EFF"
                    :unique-opened='true'
					:collapse='isCollapse'
					:collapse-transition='false'
					router
					:default-active = 'saveStatePath'

				>
					<!-- 一级菜单    :index  只支持字符串  + '' 转化成字符串-->
					<el-submenu
						:index="item.id + ''"
						v-for="(item ,i) in menuList"
						:key="item.id"
					>
						<!-- 一级菜单的模板区域 -->
						<template slot="title">
							<i class="iconfont" :class="iconsObj[i]"></i> 
							<span>{{ item.authName }}</span>
						</template>
						<!-- 二级菜单 -->
						<el-menu-item
							:index="'/' +  subItem.path"
							v-for="subItem in item.children"
							:key="subItem.id" 
							@click="saveState('/' + subItem.path)"
						>
							<template slot="title">
								<i class="el-icon-menu"></i>
								<span>{{ subItem.authName }}</span>
							</template>
						</el-menu-item>
					</el-submenu>
				</el-menu>
			</el-aside>
			<el-main>
				<!-- 路由占位符 -->
				<router-view></router-view>
			</el-main>
		</el-container>
	</el-container>
</template>

<script>
	export default {
		data() {
			return {
                menuList:[],
                //定义字体图标库
                iconsObj: [ "icon-user","icon-tijikongjian","icon-shangpin","icon-danju", "icon-baobiao"  ],
				isCollapse:false,
				saveStatePath:'', //被激活的链接地址
            }
		},
        created() {
            this.getMenList()
			this.saveStatePath = window.sessionStorage.getItem('saveState')
        },
		methods: {
			loginOut() { 
				window.sessionStorage.clear();
				this.$router.push("/login");
			},
            // 获取所有的侧别菜单
          async  getMenList() {
              const {data:res} =await this.$http.get('menus')
              console.log(res)
              if(res.meta.status !== 200) this.$message.error(res.meta.msg)
              this.menuList = res.data
            },
			// 点击按钮操作 切换菜单的折叠与展开
			toggleCollapse() {
				this.isCollapse = !this.isCollapse
			},
			// 报错链接的激活状态
			saveState(saveState) {
				window.sessionStorage.setItem('saveState',saveState)
				this.saveStatePath = saveState
			}
		},
	};
</script>

<style lang="less" scoped>
	.el-container {
		height: 100%;
	}
	.el-header {
		display: flex;
		justify-content: space-between;
		background-color: #373d41;
		align-items: center;
		color: #fff;
		font-size: 18px;
		div {
			display: flex;
			align-items: center;
			img {
				width: 40px;
				height: 40px;
			}
		}
	}
    .el-menu {
        border: 0;
    }
	.el-aside {
		background-color: #333744;
	}
	.el-main {
		background-color: #eaedf1;
	}
	.toggle-button {
		color: #fff;
		text-align: center;
		font-size: 10px;
		line-height: 24px;
		background-color: #4A5064;
		letter-spacing: 0.2em;
		cursor: pointer;
	}
</style>
